﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Grain.Crm.DataMigrator.CrmServices;

namespace Grain.Crm.DataMigrator.Imports
{
    public class ContactLanguageImport: ImportBase
    {
        public ContactLanguageImport(CrmService service, WhoAmIResponse whoAmIresponse, ImportPayload importPayload)
            : base(service, whoAmIresponse, importPayload)
        {
        }

        protected override void Preprocess(out bool entityExists)
        {
            string skillLanguage = FileContent.GetByHeading(HeaderRow, "Language Skill");

            var langSkillKeyId = ImportsHelper.GetEntityId(_service, EntityName.new_language.ToString(), new string[] { "new_name" }, new string[] { skillLanguage });

            var contLangId = ImportsHelper.GetEntityId(_service, EntityName.new_contactlanguage.ToString(), new string[] { "new_contactid", "new_languageskillid", "new_curriculumvitaeid" },
                new string[] { ImportsGlobal.ContactId, langSkillKeyId.GetValueOrDefault().ToString(), ImportsGlobal.CVId });

            HeaderRow += ", Contact, Curriculum Vitae,Language";
            FileContent += "," + ImportsGlobal.ContactId + "," + ImportsGlobal.CV+","+ImportsGlobal.Language;

            if (contLangId != null)
            {
                entityExists = true;
                ImportsHelper.AddKeyValueToContent(contLangId.GetValueOrDefault(), ThisEntityName, ref HeaderRow, ref FileContent);
            }
            else
                entityExists = false;
        }
    }
}
